package com.songtech.server.entry;

import com.songtech.server.Calculation.CenterPointCalUtils;
import com.songtech.server.Point.PosPoint;
import com.songtech.server.celltrack.model.CellTrack;
import com.songtech.server.celltrack.service.CellTrackService;
import com.songtech.server.constant.TcpServer;
import com.songtech.server.utils.CalculationUtils;
import com.songtech.server.utils.FormatDateUtils;
import com.songtech.web.entry.SysPosElefence;

import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

/**
 * Create By YINN on 2018/4/11 17:49
 * Description :
 */
public class PositionProtocalLBS extends BaseProtocal {

    private static final long serialVersionUID = -7947316958604103477L;

    //基站信息
    private String baseInfo;

    private String mccnc;

    private Integer baseNumber;

    private String[] lac;

    private String[] cellId;

    private String[] rs;

    private String[] dBm;

    private String batteryInfo;

    private String failureInfo;

    private String cont;

    private String equStatus;

    public void setAll() {

        int len = cmdParam.length;

        if (cmdParam != null && len > 0) {

            this.mccnc = cmdParam[0];

            int baseNumber = Integer.valueOf(cmdParam[1]);
            this.baseNumber = baseNumber;

            if (baseNumber > 0) {

                lac = new String[baseNumber];
                cellId = new String[baseNumber];
                rs = new String[baseNumber];
                dBm = new String[baseNumber];

                for (int i = 0; i < baseNumber; i++) {
                    lac[i] = cmdParam[i + 2];
                    cellId[i] = cmdParam[i + 3];
                    rs[i] = cmdParam[i + 4];
                    dBm[i] = cmdParam[i + 5];
                }
            }

            int begin = baseNumber * 4 + 2;
            this.batteryInfo = cmdParam[begin];
            this.dumpEnergy = CalculationUtils.getDumpEnergy(batteryInfo);
            this.failureInfo = cmdParam[begin + 1];
            //cmdParam[begin + 2] 值為x
            setCont(cmdParam[begin + 3]);
            this.cont = cmdParam[begin + 3];
            this.equStatus = cmdParam[begin + 4];
        }
    }

    /**
     * Created by YINN on 2018/5/3 15:48.
     * @Description : lbs实体转换为gps实体类方法
     * @Result : PositionProtocal
     */
    public PositionProtocal castToPositionProtocal() {

        PositionProtocal protocal = new PositionProtocal();

        protocal.setAll(manufacturer, imei, cmd, cmdTime, cmdParam);

        this.trans(protocal);

        return protocal;
    }

    private PositionProtocal trans(PositionProtocal protocal){

        /**
         *
         * 使用中心点计算
        List<PosPoint> results = new ArrayList<>();

        try{

            for (int i=0; i< lac.length; i++){

                System.out.println(lac[i]);

                System.out.println(cellId[i]);

                CellTrack cellTrack = CellTrackService.queryCellTrackInfo(lac[i],cellId[i]);

                results.add(PosPoint.instanceFactory(cellTrack));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        PosPoint point = CenterPointCalUtils.getCenterPoint(results);

         **/

        protocal.setEquStatus(this.equStatus);

        protocal.setRecordTime(FormatDateUtils.dateAndTimeToDate(LocalDate.now(), cmdTime));

        protocal.setDumpEnergy(this.dumpEnergy);

        protocal.setLocationType(TcpServer.LBS);

        protocal.setDataValidFigure("A");

        if (lac == null || lac.length < 1 || cellId == null || cellId.length < 1){
            return protocal;
        }

        try {

            //使用注册基站
            CellTrack cellTrack = CellTrackService.queryCellTrackInfo(lac[0],cellId[0]);

            PosPoint point = PosPoint.instanceFactory(cellTrack);

            protocal.setLatitude(CalculationUtils.getLatLon(point.getLat()));

            protocal.setLatitudeFlag("N");

            protocal.setLongitude(CalculationUtils.getLatLon(point.getLon()));

            protocal.setLongitudeFlag("E");

        } catch (IOException e) {

            e.printStackTrace();

        }
        return protocal;
    }


    public String getBaseInfo() {
        return baseInfo;
    }

    public void setBaseInfo(String baseInfo) {
        this.baseInfo = baseInfo;
    }

    public String getMccnc() {
        return mccnc;
    }

    public void setMccnc(String mccnc) {
        this.mccnc = mccnc;
    }

    public Integer getBaseNumber() {
        return baseNumber;
    }

    public void setBaseNumber(Integer baseNumber) {
        this.baseNumber = baseNumber;
    }

    public String[] getLac() {
        return lac;
    }

    public void setLac(String[] lac) {
        this.lac = lac;
    }

    public String[] getCellId() {
        return cellId;
    }

    public void setCellId(String[] cellId) {
        this.cellId = cellId;
    }

    public String[] getRs() {
        return rs;
    }

    public void setRs(String[] rs) {
        this.rs = rs;
    }

    public String[] getdBm() {
        return dBm;
    }

    public void setdBm(String[] dBm) {
        this.dBm = dBm;
    }

    public String getBatteryInfo() {
        return batteryInfo;
    }

    public void setBatteryInfo(String batteryInfo) {
        this.batteryInfo = batteryInfo;
    }

    public String getFailureInfo() {
        return failureInfo;
    }

    public void setFailureInfo(String failureInfo) {
        this.failureInfo = failureInfo;
    }

    public String getCont() {
        return cont;
    }

    public void setCont(String cont) {
        this.cont = cont;
    }

    public String getEquStatus() {
        return equStatus;
    }

    public void setEquStatus(String equStatus) {
        this.equStatus = equStatus;
    }


}
